package com.zhu;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResourceAccessException;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

@Service
public class AreaService {

    @Autowired
    private RestTemplate restTemplate;

    public Map<String, Object> getAreaList() {
        String url = "http://192.168.6.200:8060/lehe_entrance_guard_api/public/index.php/admin/Other/getAreaList";
        try {
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> result = objectMapper.readValue(response.getBody(), Map.class);
            return result;
        } catch (ResourceAccessException | HttpClientErrorException e) {
            // 请求超时或请求异常
            return getSampleData();
        } catch (Exception e) {
            // 其他异常
            return getSampleData();
        }
    }

    private Map<String, Object> getSampleData() {
        Map<String, Object> sampleData = new HashMap<>();
        sampleData.put("code", 200);
        sampleData.put("msg", "查询成功");
        sampleData.put("data", JSONObject.parseArray("[{\"id\":1,\"name\":\"膨化硝铵炸药制药工房\",\"limit_num\":5,\"limit_num_operate\":2,\"inUserInfo\":[{\"id\":6886,\"type\":1,\"person_id\":307,\"area_id\":1,\"personName\":\"赵富银\"},{\"id\":6939,\"type\":1,\"person_id\":344,\"area_id\":1,\"personName\":\"袁志国\"},{\"id\":6837,\"type\":4,\"person_id\":15895,\"area_id\":1,\"personName\":\"0926陌生人6\"},{\"id\":6847,\"type\":4,\"person_id\":15706,\"area_id\":1,\"personName\":\"0913陌生人10\"},{\"id\":6860,\"type\":4,\"person_id\":15618,\"area_id\":1,\"personName\":\"0908陌生人12\"},{\"id\":6863,\"type\":4,\"person_id\":15898,\"area_id\":1,\"personName\":\"0926陌生人9\"},{\"id\":6858,\"type\":4,\"person_id\":15565,\"area_id\":1,\"personName\":\"0906陌生人3\"},{\"id\":6795,\"type\":4,\"person_id\":15605,\"area_id\":1,\"personName\":\"0907陌生人18\"},{\"id\":6895,\"type\":4,\"person_id\":15899,\"area_id\":1,\"personName\":\"0926陌生人10\"},{\"id\":6914,\"type\":4,\"person_id\":15515,\"area_id\":1,\"personName\":\"0828陌生人6\"},{\"id\":6919,\"type\":4,\"person_id\":15638,\"area_id\":1,\"personName\":\"0909陌生人7\"},{\"id\":6814,\"type\":4,\"person_id\":15596,\"area_id\":1,\"personName\":\"0907陌生人9\"},{\"id\":6815,\"type\":4,\"person_id\":15892,\"area_id\":1,\"personName\":\"0926陌生人3\"},{\"id\":6940,\"type\":4,\"person_id\":15733,\"area_id\":1,\"personName\":\"0916陌生人3\"}]},{\"id\":2,\"name\":\"乳化制药装药工房\",\"limit_num\":4,\"limit_num_operate\":1,\"inUserInfo\":[{\"id\":6883,\"type\":1,\"person_id\":306,\"area_id\":2,\"personName\":\"夏泽刚\"},{\"id\":6925,\"type\":1,\"person_id\":335,\"area_id\":2,\"personName\":\"袁福刚\"},{\"id\":6930,\"type\":1,\"person_id\":322,\"area_id\":2,\"personName\":\"邢开芳\"},{\"id\":6823,\"type\":3,\"person_id\":367,\"area_id\":2,\"personName\":\"徐加位\"},{\"id\":6846,\"type\":3,\"person_id\":13630,\"area_id\":2,\"personName\":\"黄禹淞\"},{\"id\":6927,\"type\":4,\"person_id\":15887,\"area_id\":2,\"personName\":\"0925陌生人16\"},{\"id\":6928,\"type\":4,\"person_id\":15664,\"area_id\":2,\"personName\":\"0910陌生人4\"},{\"id\":6828,\"type\":4,\"person_id\":15632,\"area_id\":2,\"personName\":\"0909陌生人1\"}]},{\"id\":3,\"name\":\"返工品工房\",\"limit_num\":6,\"limit_num_operate\":3,\"inUserInfo\":[]},{\"id\":4,\"name\":\"膨化装药包装工房\",\"limit_num\":5,\"limit_num_operate\":2,\"inUserInfo\":[{\"id\":6812,\"type\":1,\"person_id\":9653,\"area_id\":4,\"personName\":\"张远江\"},{\"id\":6874,\"type\":1,\"person_id\":309,\"area_id\":4,\"personName\":\"林克金\"},{\"id\":6807,\"type\":1,\"person_id\":7140,\"area_id\":4,\"personName\":\"袁斌\"},{\"id\":6891,\"type\":1,\"person_id\":312,\"area_id\":4,\"personName\":\"张发云\"},{\"id\":6922,\"type\":1,\"person_id\":338,\"area_id\":4,\"personName\":\"王启珍\"},{\"id\":6937,\"type\":1,\"person_id\":305,\"area_id\":4,\"personName\":\"冯朝贵\"},{\"id\":6805,\"type\":4,\"person_id\":15890,\"area_id\":4,\"personName\":\"0926陌生人1\"},{\"id\":6916,\"type\":4,\"person_id\":15903,\"area_id\":4,\"personName\":\"0926陌生人14\"},{\"id\":6811,\"type\":4,\"person_id\":15791,\"area_id\":4,\"personName\":\"0922陌生人9\"},{\"id\":6789,\"type\":4,\"person_id\":15635,\"area_id\":4,\"personName\":\"0909陌生人4\"},{\"id\":6931,\"type\":4,\"person_id\":15739,\"area_id\":4,\"personName\":\"0916陌生人9\"},{\"id\":6834,\"type\":4,\"person_id\":15894,\"area_id\":4,\"personName\":\"0926陌生人5\"}]},{\"id\":5,\"name\":\"膨化硝铵炸药成品转运平台\",\"limit_num\":4,\"limit_num_operate\":1,\"inUserInfo\":[{\"id\":6923,\"type\":1,\"person_id\":332,\"area_id\":5,\"personName\":\"黄永建\"},{\"id\":6936,\"type\":1,\"person_id\":301,\"area_id\":5,\"personName\":\"冯德武\"},{\"id\":6851,\"type\":4,\"person_id\":15568,\"area_id\":5,\"personName\":\"0906陌生人6\"},{\"id\":6899,\"type\":4,\"person_id\":15901,\"area_id\":5,\"personName\":\"0926陌生人12\"},{\"id\":6917,\"type\":4,\"person_id\":15439,\"area_id\":5,\"personName\":\"0820陌生人10\"},{\"id\":6809,\"type\":4,\"person_id\":15891,\"area_id\":5,\"personName\":\"0926陌生人2\"},{\"id\":6924,\"type\":4,\"person_id\":15836,\"area_id\":5,\"personName\":\"0922陌生人54\"},{\"id\":6932,\"type\":4,\"person_id\":15425,\"area_id\":5,\"personName\":\"0819陌生人6\"},{\"id\":6831,\"type\":4,\"person_id\":15527,\"area_id\":5,\"personName\":\"0902陌生人2\"},{\"id\":6938,\"type\":4,\"person_id\":15652,\"area_id\":5,\"personName\":\"0909陌生人21\"}]},{\"id\":6,\"name\":\"乳化包装工房\",\"limit_num\":5,\"limit_num_operate\":2,\"inUserInfo\":[{\"id\":6842,\"type\":1,\"person_id\":329,\"area_id\":6,\"personName\":\"李龙\"},{\"id\":6929,\"type\":3,\"person_id\":369,\"area_id\":6,\"personName\":\"曹亚军\"},{\"id\":6869,\"type\":4,\"person_id\":15746,\"area_id\":6,\"personName\":\"0918陌生人2\"}]},{\"id\":7,\"name\":\"乳化炸药成品转运平台\",\"limit_num\":4,\"limit_num_operate\":1,\"inUserInfo\":[]},{\"id\":9,\"name\":\"成品输送廊道\",\"limit_num\":4,\"limit_num_operate\":1,\"inUserInfo\":[{\"id\":6878,\"type\":4,\"person_id\":328,\"area_id\":9,\"personName\":\"陈福友\"}]}]"));
        return sampleData;
    }
}

